// baseURL
function domain() {
	// return 'https://www.fastmock.site/mock/7c6746ebff157bfd5a5834a5b3e65110/xy';
	return 'https://xingyue.heartreading.cn';
}
function formatTime(time) {
	if (typeof time !== 'number' || time < 0) {
		return time
	}

	var hour = parseInt(time / 3600)
	time = time % 3600
	var minute = parseInt(time / 60)
	time = time % 60
	var second = time

	return ([hour, minute, second]).map(function(n) {
		n = n.toString()
		return n[1] ? n : '0' + n
	}).join(':')
}

function formatLocation(longitude, latitude) {
	if (typeof longitude === 'string' && typeof latitude === 'string') {
		longitude = parseFloat(longitude)
		latitude = parseFloat(latitude)
	}

	longitude = longitude.toFixed(2)
	latitude = latitude.toFixed(2)

	return {
		longitude: longitude.toString().split('.'),
		latitude: latitude.toString().split('.')
	}
}
var dateUtils = {
	UNITS: {
		'年': 31557600000,
		'月': 2629800000,
		'天': 86400000,
		'小时': 3600000,
		'分钟': 60000,
		'秒': 1000
	},
	humanize: function(milliseconds) {
		var humanize = '';
		for (var key in this.UNITS) {
			if (milliseconds >= this.UNITS[key]) {
				humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前';
				break;
			}
		}
		return humanize || '刚刚';
	},
	format: function(dateStr) {
		var date = this.parse(dateStr)
		var diff = Date.now() - date.getTime();
		if (diff < this.UNITS['天']) {
			return this.humanize(diff);
		}
		var _format = function(number) {
			return (number < 10 ? ('0' + number) : number);
		};
		return date.getFullYear() + '/' + _format(date.getMonth() + 1) + '/' + _format(date.getDay()) + '-' +
			_format(date.getHours()) + ':' + _format(date.getMinutes());
	},
	parse: function(str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串，转化为一个Date对象
		var a = str.split(/[^0-9]/);
		return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
	}
};
function ly_request(
	url,
	method = "GET",
	data
) {
	// 开始请求
	url = domain() + url ;
	// 获取本地保存的session_id加入到每次请求中
	var session_id = uni.getStorageSync('PHPSESSID'); //本地取存储的sessionID
	if (session_id != "" && session_id != null) {
		var header = {
			'X-Requested-With': 'xmlhttprequest',
			'Lingyun-Api': 'Lingyun-Api',
			'content-type': 'application/x-www-form-urlencoded',
			'Cookie': 'PHPSESSID=' + session_id,
			'Session-Id': session_id
		}
	} else {
		var header = {
			'X-Requested-With': 'xmlhttprequest',
			'Lingyun-Api': 'Lingyun-Api',
			'content-type': 'application/x-www-form-urlencoded'
		}
	}
	let promise = new Promise((resolve,reject)=>{
		uni.request({
				url: url,
				method: method,
				data: data,
				header: header,
				dataType: 'json',
				success: res => {
					let data = res.data
					resolve(data)
					console.log('ly_request:' + 'success:' + url);
					// if (session_id == "" || session_id == null) {
					// 	// 如果本地没有就说明第一次请求 把返回的session id 存入本地
					// 	uni.setStorageSync('PHPSESSID', res.data.session_id)
					// }
		// 			if (res.data.login == '1') {
		// 				uni.reLaunch({
		// 					url: '../../user/center/login'
		// 				})
		// 			}
					// success(res); // 调用用户success方法
				},
				fail: error=>{
					// self.message("网络中断或服务不存在");
					console.log(error);
					reject(error);
				},
				// complete: complete
			})
	});
		return promise
	// 发起请求
	console.log('session_id:' + session_id);
	
}

/* 时间戳转格式 */
function formatNumber(n) {
	n = n.toString();
	return n[1] ? n : '0' + n;
}

function formatdate(number, format) {

	var formateArr = ['Y', 'M', 'D', 'h', 'm', 's']; //
	var returnArr = [];

	var date = new Date(number * 1000);
	returnArr.push(date.getFullYear());
	returnArr.push(formatNumber(date.getMonth() + 1));
	returnArr.push(formatNumber(date.getDate()));


	returnArr.push(formatNumber(date.getHours()));
	returnArr.push(formatNumber(date.getMinutes()));
	returnArr.push(formatNumber(date.getSeconds()));


	for (var i in returnArr) {
		format = format.replace(formateArr[i], returnArr[i]);
	}
	//format.replace(/\//g,'-');
	return format;
}


function add0(m){return m<10?'0'+m:m }
function formattimetime(shijianchuo)
{
//shijianchuo是整数，否则要parseInt转换
if(shijianchuo==0){
	return ''
}else{
	var time = new Date(parseInt(shijianchuo)*1000);
	var y = time.getFullYear();
	var m = time.getMonth()+1;
	var d = time.getDate();
	var h = time.getHours();
	var mm = time.getMinutes();
	var s = time.getSeconds();
	return y+'.'+add0(m)+'.'+add0(d);
}

// return y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm)+':'+add0(s);
}
function formattimetime1(shijianchuo)
{
//shijianchuo是整数，否则要parseInt转换
var time = new Date(parseInt(shijianchuo));
var y = time.getFullYear();
var m = time.getMonth()+1;
var d = time.getDate();
var h = time.getHours();
var mm = time.getMinutes();
var s = time.getSeconds();
return y+'-'+add0(m)+'-'+add0(d);
// return y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm)+':'+add0(s);
}
function formattimetime2(shijianchuo)
{
//shijianchuo是整数，否则要parseInt转换
var time = new Date(parseInt(shijianchuo));
var y = time.getFullYear();
var m = time.getMonth()+1;
var d = time.getDate();
var h = time.getHours();
var mm = time.getMinutes();
var s = time.getSeconds();
return y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm);
}
function formattimetime4(shijianchuo)
{
//shijianchuo是整数，否则要parseInt转换
if(shijianchuo==0){
	return ''
}else{
	var time = new Date(parseInt(shijianchuo)*1000);
	var y = time.getFullYear();
	var m = time.getMonth()+1;
	var d = time.getDate();
	var h = time.getHours();
	var mm = time.getMinutes();
	var s = time.getSeconds();
	return y;
}
}


function getQueryString(name){
    var reg = new RegExp("\\b"+ name +"=([^&]*)");
    var r = location.href.match(reg);
    if (r!=null) return decodeURIComponent(r[1]);
}

module.exports = {
	formatTime: formatTime,
	formatLocation: formatLocation,
	dateUtils: dateUtils,
	ly_request: ly_request,
	formatdate: formatdate,
	domain: domain,
	formattimetime:formattimetime,
	formattimetime1:formattimetime1,
	formattimetime2:formattimetime2,
	formattimetime4:formattimetime4,
	getQueryString
}
